package com.truongnha.clienttn.data.dao;

import java.util.ArrayList;
import java.util.List;

import com.truongnha.clienttn.data.entity.StudentSubjectEntity;

import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

public class StudentSubjectDAO {
	private final SQLiteDatabase db;

	public StudentSubjectDAO(SQLiteDatabase db) {
		super();
		this.db = db;
	}

	public long insert(int markId, String name) {
		ContentValues inititalValue = new ContentValues();
		inititalValue.put(StudentSubjectScheme.MARK_ID, markId);
		inititalValue.put(StudentSubjectScheme.NAME, name);
		return db.insert(StudentSubjectScheme.TABLE_NAME, null, inititalValue);
	}

	public void delete() {
		db.delete(StudentSubjectScheme.TABLE_NAME, null, null);
	}

	public List<StudentSubjectEntity> getAll() {
		String sql = "SELECT * from " + StudentSubjectScheme.TABLE_NAME;
		Cursor mcursor = db.rawQuery(sql, null);
		List<StudentSubjectEntity> list = new ArrayList<StudentSubjectEntity>();
		if (mcursor.getCount() != 0) {			
			mcursor.moveToFirst();
			while (!mcursor.isAfterLast()) {
				StudentSubjectEntity attendEntity = new StudentSubjectEntity(
						mcursor.getInt(0), mcursor.getInt(1),
						mcursor.getString(2));
				list.add(attendEntity);
				mcursor.moveToNext();
			}
		}
		mcursor.close();		
		return list;
	}
}
